<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/7/27
 * Time: 上午6:46
 */

namespace App\Services;


use App\Models\Apart;
use App\Models\Repair;
use App\Models\Teacher;
use App\Models\User;

class RepairService extends Service
{

    public static function getRepairList($limit, $offset, $order = 'desc', $option = [])
    {
        $query = Repair::where('status', '>', 0);
        if (count($option) && count($option['query'])) {
            foreach ($option['query'] as $search) {
                if ($search['key'] == 'apart_id') {
                    $query = $query->whereIn($search['key'], $search['value']);
                } else {
                    $query = $query->where($search['key'], $search['operator'], $search['value']);
                }
            }
        }
        $total = $query->count();
        $repairs = $query->orderBy('id', $order)->limit($limit)->offset($offset)->get();
        $data = [];
        foreach ($repairs as $repair) {
            $apart = Apart::where('id', $repair->apart_id)->first();
            $data[] = [
                'id' => $repair->id,
                'apart_id' => $apart->id,
                'name' => $apart->name,
                'type' => $repair->type,
                'content' => $repair->content,
                'status' => $repair->status,
                'reply_at' => $repair->reply_at
            ];
        }
        return [
            'total' => $total,
            'rows' => $data,
            'limit' => $limit,
            'offset' => $offset,
            'page' => ceil($total / $limit),
            'pageNumber' => $offset / $limit + 1
        ];
    }

    public static function getRepairDetail($id)
    {
        $repair = Repair::where('id', $id)->first();

        if (!$repair) {
            return [];
        }
        $apart = Apart::where('id', $repair->apart_id)->first();
        $user = User::where('id', $repair->user_id)->first();

        return [
            'data' => [
                'id' => $repair->id,
                'name' => $apart->name,
                'rent_user_number' => $user->number,
                'type' => $repair->type,
                'status' => $repair->status,
                'reply_at' => $repair->reply_at,
                'mobile' => $repair->mobile,
                'content' => $repair->content,
                'remark' => $repair->remark,
                'rent_user_name' => $user->realname
            ]
        ];
    }

    public static function saveRepair($data)
    {
        $repair = new Repair();
        if (array_key_exists('id', $data) && $data['id']) {
            $repair = Repair::where('id', $data['id'])->first();
        } else {
            $repair->created_at = date('Y-m-d H:i:s', time());
        }
        foreach ($data as $key => $value) {
            $repair->$key = $value;
        }
        $repair->updated_at = date('Y-m-d H:i:s', time());
        $repair->save();
    }

    public static function deleteRepair($id)
    {
        $repair = Repair::where('id', $id)->first();
        $repair->status = 0;
        $repair->updated_at = date('Y-m-d H:i:s', time());
        $repair->save();
    }

    public static function changeRepairStatus($data)
    {
        $repair = Repair::where('id', $data['id'])->first();
        $repair->status = $data['status'];
        if ($repair->status == 2) {
            $repair->reply_at = date('Y-m-d H:i:s', time());
        }
        $repair->remark = $data['remark'];
        $repair->updated_at = date('Y-m-d H:i:s', time());
        $repair->save();
    }
}